package com.oa.tools.jobs;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.oa.tools.TkMapper;
import com.oa.tools.entity.FlowLog;
import com.oa.tools.entity.LbpmProcess;
import com.oa.tools.entity.OAFlowInstanceWithBLOBs;
import com.oa.tools.entity.OrgElement;
import com.oa.tools.entity.Person;
import com.oa.tools.entity.ProjectAccount;
import com.oa.tools.entity.Stamp;
import com.oa.tools.utils.FileUtil;
import com.oa.tools.utils.MailUtil;
import com.oa.tools.utils.SysConfig;

import tk.mybatis.mapper.entity.Example;

public abstract class FlowHandlerTaskBase {
	
	public static final String FLOW_STATUS_DONE="30";
	public static final String FLOW_STATUS_ING="20";
	public static final String FLOW_STATUS_ERROR="21";
	public static final String FLOW_STATUS_DRAFT="10";
	public static final String FLOW_STATUS_DISCARD="00";
	
	protected TkMapper<OAFlowInstanceWithBLOBs> oAFlowInstanceDAO;
	protected TkMapper<FlowLog> flowLogDAO;
	protected TkMapper<Person> personDAO;
	protected TkMapper<Stamp> stampDAO;
	protected TkMapper<OrgElement> orgElementDAO;
	protected TkMapper<LbpmProcess> lbpmProcessDAO;
	protected TkMapper<ProjectAccount> projectAccountDAO;
	protected TkMapper<ProjectAccount> orgElementWithPersonDAO;
	
	protected String templateId;
	protected String flowStatus;
	
	@SuppressWarnings("unchecked")
	public FlowHandlerTaskBase(Map<String,TkMapper<?>> daoMap) {
		super();
		this.oAFlowInstanceDAO = (TkMapper<OAFlowInstanceWithBLOBs>) daoMap.get("OAFlowInstanceDAO");
		this.flowLogDAO = (TkMapper<FlowLog>) daoMap.get("FlowLogDAO");
		this.personDAO = (TkMapper<Person>) daoMap.get("PersonDAO");
		this.stampDAO = (TkMapper<Stamp>) daoMap.get("StampDAO");
		this.orgElementDAO = (TkMapper<OrgElement>) daoMap.get("OrgElementDAO");
		this.lbpmProcessDAO = (TkMapper<LbpmProcess>) daoMap.get("LbpmProcessDAO");
		this.projectAccountDAO = (TkMapper<ProjectAccount>) daoMap.get("ProjectAccountDAO");
		this.orgElementWithPersonDAO =  (TkMapper<ProjectAccount>) daoMap.get("OrgElementWithPersonDAO");
		
	}

	public String start(boolean notice,boolean force,long lastestDateMill) throws Exception {
		Example example = new Example(OAFlowInstanceWithBLOBs.class);
		example.createCriteria().andEqualTo("docStatus", flowStatus).andGreaterThan("fdLastModifiedTime", new Date(lastestDateMill)).andEqualTo("fdTemplateId",templateId);
		StringBuilder sb = null;
		List<OAFlowInstanceWithBLOBs> objs = this.oAFlowInstanceDAO.selectByExample(example);
		if(objs!=null) {
			sb = new StringBuilder();
			for(OAFlowInstanceWithBLOBs obj:objs) {
				sb.append(startOne(notice,force,obj));
			}
		}
		
		return sb==null?"":sb.toString();
	}

	public String startOne(boolean notice,boolean force,OAFlowInstanceWithBLOBs obj) throws Exception {
		if(templateId==null || !templateId.equals(obj.getFdTemplateId())) {
			return null;
		}
		
		String recordPath = SysConfig.getSysString("流程处理记录路径")+"/"+this.getClass().getName()+"_"+obj.getFdId()+".txt";
		File oldFile  = new File(recordPath);
		if(oldFile.exists()) {
			if(!force) {
				return null;
			}
			oldFile.renameTo(new File(recordPath.replace(".txt", "_bak"+System.currentTimeMillis()+".txt")));
		}
		String result = exec(notice, obj);
		
		if(result!=null) {
			FileUtil.writeTxt2File(result, recordPath);
			if(notice) {
				String [] receiveMailAccounts = SysConfig.getSysSet("管理员接收邮箱").toArray(new String[0]);
				MailUtil.sendMail(receiveMailAccounts,
						obj.getDocSubject()+"流程自动处理通知", new StringBuilder().append("处理信息如下：\r\n<pre>").append(result).append("</pre>").toString().replace("][", "\r\n"));
			}
		}
		
		return result;
	
	}
	
	
	public abstract String exec(boolean notice,OAFlowInstanceWithBLOBs obj) throws Exception;
	
}
